using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using System;
using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Dialogs;
using MS.WindowsAPICodePack.Internal;


public partial class ImageDialog
{

    public string Res = "Cancel";
    public System.Collections.Specialized.StringCollection Images = new System.Collections.Specialized.StringCollection();

    public ImageDialog()
    {
        InitializeComponent();
    }
    public void OKButton_Click(object sender, RoutedEventArgs e)
    {
        Res = "OK";
        Close();
    }

    public void ImageDialog_Loaded(object sender, RoutedEventArgs e)
    {
        if ((new Microsoft.VisualBasic.Devices.Computer()).Clipboard.ContainsImage())
        {
            FromClipboardButton.IsEnabled = true;
        }
        else
        {
            FromClipboardButton.IsEnabled = false;
        }
        FromFileButton.IsChecked = true;
        if (MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnVista || MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7)
        {
            AppHelper.ExtendGlassFrame(this, new Thickness(-1, -1, -1, -1));
        }
    }

    public void FromClipboardButton_Checked(System.Object sender, RoutedEventArgs e)
    {
        AddButton.IsEnabled = false;
        var b = Clipboard.GetImage();
        ImagePreview.Height = b.Height;
        ImagePreview.Width = b.Width;
        ImagePreview.Source = b;
        OKButton.IsEnabled = true;
    }

    public void AddButton_Click(System.Object sender, RoutedEventArgs e)
    {
        if (CoreHelpers.RunningOnVista || CoreHelpers.RunningOnWin7)
        {
            var openfile = new CommonOpenFileDialog();
            CommonFileDialogFilterCollection fil;
            openfile.Multiselect = true;
            openfile.Title = "Insert Image";
            fil = openfile.Filters;
            var SupportedFiles = new CommonFileDialogFilter("Supported Images", "*.bmp;*.gif;*.jpeg;*.jpg;*.png");
            var BitmapFiles = new CommonFileDialogFilter("Bitmap Images", "*.bmp");
            var GifFiles = new CommonFileDialogFilter("Gif Images", "*.gif");
            var JpgFiles = new CommonFileDialogFilter("Jepg Images", "*.jpeg;*.jpg");
            var PngFiles = new CommonFileDialogFilter("Png Images", "*.png");
            fil.Add(SupportedFiles);
            fil.Add(BitmapFiles);
            fil.Add(GifFiles);
            fil.Add(JpgFiles);
            fil.Add(PngFiles);
            Topmost = false;
            if (openfile.ShowDialog() == CommonFileDialogResult.Ok)
            {
                FileBox.Items.Clear();
                Images.Clear();
                foreach (string i in openfile.FileNames)
                {
                    Images.Add(i);
                    FileBox.Items.Add(i);
                }
                FileBox.SelectedIndex = 0;
                var b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(FileBox.SelectedItem.ToString());
                b.EndInit();
                ImagePreview.Height = b.Height;
                ImagePreview.Width = b.Width;
                ImagePreview.Source = b;
                OKButton.IsEnabled = true;
            }
            Topmost = true;
        }
        else
        {
            var open = new Microsoft.Win32.OpenFileDialog();
            open.Filter = "Supported Images|*.bmp;*.gif;*.jpeg;*.jpg;*.png|Bitmap Images|*.bmp|Gif Images|*.gif|Jpeg Images|*.jpeg;*.jpg|Png Images|*.png";
            if (open.ShowDialog() == true)
            {
                this.Focus();
                Images.Clear();
                FileBox.Items.Clear();
                foreach (string i in open.FileNames)
                {
                    Images.Add(i);
                    FileBox.Items.Add(i);
                }
                FileBox.SelectedIndex = 0;
                var b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(FileBox.SelectedItem.ToString());
                b.EndInit();
                ImagePreview.Height = b.Height;
                ImagePreview.Width = b.Width;
                ImagePreview.Source = b;
                OKButton.IsEnabled = true;
            }
            this.Focus();
        }
    }

    public void FromFileButton_Checked(object sender, RoutedEventArgs e)
    {
        if (FileBox.Items.Count == 0)
        {
            ImagePreview.Source = null;
            OKButton.IsEnabled = false;
        }
        else
        {
            var b = new BitmapImage();
            b.BeginInit();
            b.UriSource = new Uri(FileBox.SelectedItem.ToString());
            b.EndInit();
            ImagePreview.Source = b;
        }
        AddButton.IsEnabled = true;
    }

    public void FileBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (FileBox.SelectedItem != null)
        {
            var b = new BitmapImage();
            b.BeginInit();
            b.UriSource = new Uri(FileBox.SelectedItem.ToString());
            b.EndInit();
            ImagePreview.Source = b;
        }
        else
        {
            ImagePreview = null;
        }
    }
}

